package ph.com.gs3.formalistics;

import ph.com.gs3.formalistics.fragments.workers.TesterWorkerFragment;
import ph.com.gs3.formalistics.fragments.workers.TesterWorkerFragment.TesterWorkerCallbacks;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class TesterActivity extends Activity implements TesterWorkerCallbacks {

	private TesterWorkerFragment workerFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tester);

		FragmentManager fragmentMan = getFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentMan.beginTransaction();

		workerFragment = (TesterWorkerFragment) fragmentMan
		        .findFragmentByTag(TesterWorkerFragment.TAG);

		if (savedInstanceState == null) {
			fragmentTransaction.add(R.id.container, new PlaceholderFragment());
		}

		if (workerFragment == null) {
			workerFragment = new TesterWorkerFragment();
			fragmentTransaction.add(workerFragment, TesterWorkerFragment.TAG);
		}

		fragmentTransaction.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tester, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		Button bStartBackgroundTask;
		Button bShowNotification;
		Button bShowTestDocumentView;

		public PlaceholderFragment() {}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
		        Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_tester, container, false);

			bStartBackgroundTask = (Button) rootView.findViewById(R.id.tester_bStartBackgroundTask);

			bStartBackgroundTask.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					TesterActivity parent = (TesterActivity) getActivity();
					parent.workerFragment.startTask();
				}
			});

			bShowNotification = (Button) rootView.findViewById(R.id.tester_bShowNotification);

			bShowNotification.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					NotificationCompat.Builder builder = new NotificationCompat.Builder(
					        getActivity()).setSmallIcon(R.drawable.ic_launcher)
					        .setContentTitle("My notification").setContentText("Hello World!");
					// Creates an explicit intent for an Activity in your app
//					Intent resultIntent = new Intent(getActivity(), MainActivity.class);

					NotificationManager notificationManager = (NotificationManager) getActivity()
					        .getSystemService(Context.NOTIFICATION_SERVICE);

					notificationManager.notify(3013, builder.build());

				}
			});

			bShowTestDocumentView = (Button) rootView
			        .findViewById(R.id.tester_bShowTestDocumentView);

			bShowTestDocumentView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

				}
			});

			return rootView;
		}
	}

	// ========================================================================
	// Implementation

	@Override
	public void onProcessFinished() {
		Toast.makeText(TesterActivity.this, "test", Toast.LENGTH_LONG).show();
	}

}
